<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>
<%@ page import="com.google.appengine.api.xmpp.Message" %>
<%@ page import="com.google.appengine.api.xmpp.MessageBuilder" %>
<%@ page import="com.google.appengine.api.xmpp.MessageType" %>
<%@ page import="com.google.appengine.api.xmpp.XMPPService" %>
<%@ page import="com.google.appengine.api.xmpp.XMPPServiceFactory" %>
<%@ page import="com.google.appengine.api.xmpp.JID" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Video Chat</title>

  <script src="http://staging.tokbox.com/v0.91/js/TB.min.js"></script>

  <script type="text/javascript">
    //This is the vid chat api key that tokbox has
    var apiKey = '7297201';
    
    <% 
    String sessionId = "";
    if (request.getParameter("sessionId") != null) {
    	sessionId = request.getParameter("sessionId");
    	out.print("var sessionId = '" + sessionId + "';");
    	
    	//Need an if statement around this that will only call it if it is a student
        //connecting
    	XMPPService xmppService = XMPPServiceFactory.getXMPPService();
        String m = "d4dietri " + "would like to video chat: " + "helloworld-david.appspot.com/vidchat.jsp?sessionId=" + request.getParameter("sessionId");
        Message msg = new MessageBuilder()
        	.withRecipientJids(new JID("david.drich@gmail.com"))
        	.withMessageType(MessageType.CHAT)
        	.withBody(m)
        	.build();
        		
       	xmppService.sendMessage(msg);
    }
    
%>
    
    var token = 'devtoken';
     
    TB.setLogLevel(TB.DEBUG);
 
    var session = TB.initSession(sessionId);     
    session.addEventListener('sessionConnected', sessionConnectedHandler);
    session.addEventListener('streamCreated', streamCreatedHandler);     
    session.connect(apiKey, token);
 
    var publisher;
 
    function sessionConnectedHandler(event) {
      publisher = session.publish('myPublisherDiv');
       
      // Subscribe to streams that were in the session when we connected
      subscribeToStreams(event.streams);
    }
     
    function streamCreatedHandler(event) {
      // Subscribe to any new streams that are created
      subscribeToStreams(event.streams);
    }
     
    function subscribeToStreams(streams) {
      for (var i = 0; i < streams.length; i++) {
        // Make sure we don't subscribe to ourself
        if (streams[i].connection.connectionId == session.connection.connectionId) {
          return;
        }
 
        // Create the div to put the subscriber element in to
        var div = document.createElement('div');
        div.setAttribute('id', 'stream' + streams[i].streamId);
        document.body.appendChild(div);
                           
        // Subscribe to the stream
        session.subscribe(streams[i], div.id);
      }
    }
  </script>

</head>
<body>
	<div style="margin:auto;padding:200px;">
	<div id="myPublisherDiv"></div>
	</div>
</body>
</html>